package com.android.bandroid.plugins;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.bandroid.R;

/**
 * 页面顶部管理
 */
public class TopBarManage {

    private View topBarView;
    private TextView titleTxt;
    private Button leftBtn, leftBtnTwo;
    private Button rightBtn, rightBtnTwo, rightBtnThree;
    private ProgressBar progress;
    private Activity mActivity;
    private RelativeLayout leftLogoLlayout;

    public RelativeLayout getLeftLogoLlayout() {
        return leftLogoLlayout;
    }

    /**
     * 构造函数，传人需要管理的头部view
     *
     * @param view
     */
    public TopBarManage(View view, Activity activity) {
        commonInit(view);
        this.mActivity = activity;
    }

    /**
     * 获取头部导航栏内部的控件信息
     *
     * @param view
     */
    private void commonInit(View view) {
        this.topBarView = view;
        titleTxt = (TextView) topBarView.findViewById(R.id.txt_top_bar_title);
        leftBtn = (Button) topBarView.findViewById(R.id.btn_top_bar_left);
        leftBtnTwo = (Button) topBarView.findViewById(R.id.btn_top_bar_left_two);
        rightBtn = (Button) topBarView.findViewById(R.id.btn_top_bar_right);
        rightBtnTwo = (Button) topBarView.findViewById(R.id.btn_top_bar_right_two);
        rightBtnThree = (Button) topBarView.findViewById(R.id.btn_top_bar_right_three);
        progress = (ProgressBar) topBarView.findViewById(R.id.progress_top_bar);
        leftLogoLlayout = (RelativeLayout) topBarView.findViewById(R.id.llayout_top_bar_menu);
        //默认左侧点击为返回
        leftBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
        leftBtnTwo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
    }

    /**
     * 左侧改为叉
     */
    public void setLeftButtonTwo(boolean isShow) {
        if (isShow) {
            leftBtn.setVisibility(View.GONE);
            leftBtnTwo.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 菜单页顶部头像，并执行相应的点击方法
     */
    public void setLeftButtonMenu(boolean isShow) {
        if (isShow) {
            leftBtn.setVisibility(View.GONE);
            leftBtnTwo.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 初始化导航栏名称
     *
     * @param title
     */
    public void initTopBarTitle(String title) {
        titleTxt.setText(title);
    }

    /**
     * 加载动画
     *
     * @param title
     */
    public void TopProgress(boolean title) {

        if (title) {
            progress.setVisibility(View.VISIBLE);
        } else {
            progress.setVisibility(View.GONE);
        }
    }

    /**
     * 设置导航栏左边按钮
     *
     * @param txt
     * @param isShow
     */
    public void setLeftButton(String txt, boolean isShow) {
        leftBtn.setText(txt);
        setLeftButton(isShow);
    }

    /**
     * 默认显示，传入false隐藏并取消点击效果
     *
     * @param isShow
     */
    public void setLeftButton(boolean isShow) {
        leftBtn.setText("");
        if (!isShow) {
            leftBtn.setVisibility(View.GONE);
            return;
        } else {
            leftBtn.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置导航栏右边按钮
     *
     * @param txt
     * @param isShow
     * @param listener
     */
    public void setRightBtn(String txt, boolean isShow, OnClickListener listener) {
        setButton(rightBtn, txt, isShow, listener);
    }

    public void setRightBtn(boolean isShow, OnClickListener listener) {
        setButton(rightBtn, isShow, listener);
    }

    public void setRightBtn(String txt, Boolean isShow) {
        rightBtn.setText(txt);
        if (isShow) {
            rightBtn.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置Button的样式  更改button的文字内容
     *
     * @param btn
     * @param txt
     * @param isShow
     * @param listener
     */
    private void setButton(Button btn, String txt, boolean isShow, OnClickListener listener) {
        if (!isShow) {
            btn.setVisibility(View.GONE);
            return;
        }
        btn.setText(txt);
        btn.setVisibility(View.VISIBLE);
        btn.setOnClickListener(listener);
    }

    /**
     * 设置Button的样式  使用默认文字“返回”
     *
     * @param btn
     * @param isShow
     * @param listener
     */
    private void setButton(Button btn, boolean isShow, OnClickListener listener) {
        if (!isShow) {
            btn.setVisibility(View.GONE);
            return;
        }
        btn.setVisibility(View.VISIBLE);
        btn.setOnClickListener(listener);
    }

    /**
     * 设置主菜单左侧头像显示并绑定点击事件
     */
    public void setLeftButtonMenu(OnClickListener listener) {
        leftBtn.setVisibility(View.GONE);
        leftLogoLlayout.setVisibility(View.VISIBLE);
        leftLogoLlayout.setOnClickListener(listener);
    }
}
